package com.woniuxy.springsecurity.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author author
 * @Create 2023-10-26 15:49
 */
@JsonIgnoreProperties({"authorities","password","username","accountNonExpired","accountNonLocked","credentialsNonExpired","enabled"})
@NoArgsConstructor
@AllArgsConstructor
@Data
public class LoginUserDetails implements UserDetails {

    //从数据库查询的用户信息
    private User user;
    private List<String> permissions;

    //后面授权要使用的
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
//        List<GrantedAuthority> authorities=new ArrayList<>();
//        permissions.stream()
//                .forEach(s -> {
//                    SimpleGrantedAuthority authority = new SimpleGrantedAuthority(s);
//                    authorities.add(authority);
//                });
//        return authorities;

//        return permissions.stream()
//                .map(str->new SimpleGrantedAuthority(str))
//                .collect(Collectors.toList());


        return permissions.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
    }

    //前端传递过来的密码作比较，一定要获取查询密码
    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUname();
    }

    //这些判断以后再Filter通过token实现，都让他们返回true
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
